const dataBase = require('../../mysql')

const sql = require('../../mysql/sql')
const verify = require('../../auth/verify')
const verifyRequestMethod = require('../../tool/verifyRequestMethod')

/**
 * @description 关注
 */
module.exports = function () {
    const that = this
    /**
     * @description 获取query
     * @type {{}|*}
     */
    const query = this.getQueryData(this.request.url)
    const authorization = this.request.headers.authorization
    /**
     * @description 请求验证
     */
    verifyRequestMethod(this.request, 'GET').then(function () {
        /**
         * @description 授权验证
         */
        if (that.isDef(authorization)) {
            if (!isNaN(parseInt(query.targetId))) {
                return verify(authorization)
            } else {
                return Promise.reject(new Error('无效的目标Id！'))
            }
        } else {
            return Promise.reject(new Error('缺少Authorization！'))
        }
    }).then(function (data) {
        return dataBase.connect(sql.SEL_SELECT_USER_ONE(data.username))
    }).then(function (result) {
        /**
         * @description 当前的用户
         */
        if (that.isDef(result)) {
            const currentUser = result[0]
            /**
             * @description 当前用户的订阅数
             */
            const subs = currentUser.subs
            /**
             * @description 当前用户的用户ID
             * @type {module.exports.register.body.username|{type, required}|module.exports.login.query.username|string|{type: NumberConstructor, required: boolean}}
             */
            const username = currentUser.username
            /**
             * @description 存储将要订阅的用户
             * @type {Array}
             */
            let setSubs = []
            /**
             * @description 如果没有订阅过?
             */
            if (subs === null) {
                setSubs.push(parseInt(query.targetId))
                /**
                 * @description 如果已经订阅
                 */
            } else if (Array.isArray(JSON.parse(subs))) {
                const currentSubs = JSON.parse(subs)
                /**
                 * @description 如果不包含已经订阅的用户
                 */
                if (!currentSubs.includes(parseInt(query.targetId))) {
                    setSubs = setSubs.concat(currentSubs, parseInt(query.targetId))
                } else {
                    /**
                     * @description 如果已包含?
                     */
                    setSubs = currentSubs.filter(function (item) {
                        return item !== parseInt(query.targetId)
                    })
                }
            }
            /**
             * @description 订阅成功?
             */
            if (setSubs.length !== 0) {
                dataBase.connect(sql.SEL_SELECT_USER_ONE(query.targetId)).then(function (result) {
                    /**
                     * @description 被订阅的用户
                     */
                    if (that.isDef(result[0])) {
                        const currentTarget = result[0]
                        /**
                         * @description 获取粉丝数组
                         */
                        const fans = currentTarget.fans
                        /**
                         * @description 存储粉丝
                         * @type {Array}
                         */
                        let setFans = []
                        /**
                         * @description 没有粉丝?
                         */
                        if (fans === null) {
                            setFans.push(username)
                            /**
                             * @description 有粉丝?
                             */
                        } else if (Array.isArray(JSON.parse(fans))) {
                            const currentFans = JSON.parse(fans)
                            /**
                             * @description 如果不包含某个粉丝
                             */
                            if (!currentFans.includes(username)) {
                                setFans = setFans.concat(currentFans, username)
                            } else {
                                /**
                                 * @description 如果包含某个粉丝
                                 */
                                setFans = currentFans.filter(function (item) {
                                    return item !== username
                                })
                            }
                        }
                        /**
                         * @description 更新订阅数据
                         */
                        dataBase.connect(sql.SQL_UPDATE_USER_SUB(username, setSubs.toString(), setSubs.length)).then(function (result) {
                            /**
                             * @description 更新粉丝数据
                             */
                            return dataBase.connect(sql.SQL_UPDATE_USER_FANS(query.targetId, setFans.toString(), setFans.length))
                        }).then(function () {
                            that.response.outPrintData({}, '成功！')
                        })
                    }
                })
            }
        }
    }).catch(function (e) {
        that.response.outPrintError((e && e.message) || '未知错误！')
    })
}